package com.yy.job.console.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yy.job.common.dao.YYJobStepDOMapper;
import com.yy.job.common.dao.YYJobStepLogDOMapper;
import com.yy.job.common.dao.po.YYJobStepDO;
import com.yy.job.common.dao.po.YYJobStepDOExample;
import com.yy.job.common.dao.po.YYJobStepLogDO;
import com.yy.job.common.dao.po.YYJobStepLogDOExample;
import com.yy.job.console.converter.JobStepLogViewConverter;
import com.yy.job.console.dto.JobStepLogView;

@Service
public class JobStepLogConsoleService {
	
	@Autowired
	private YYJobStepLogDOMapper stepLogDOMapper;
	
	@Autowired
	private YYJobStepDOMapper jobStepDOMapper;
	
	public List<JobStepLogView> listJobStepLogs(Long jobLogId, Map<Long, YYJobStepDO> jobStepMap) {
		// 查询step日志
		YYJobStepLogDOExample example = new YYJobStepLogDOExample();
		example.createCriteria().andJobLogIdEqualTo(jobLogId).andIsDeletedEqualTo("N");
		example.setOrderByClause("gmt_created asc");
		List<YYJobStepLogDO> jobStepLogList = stepLogDOMapper.selectByExample(example);
		
		return JobStepLogViewConverter.listConvert(jobStepLogList, jobStepMap);
	}
	
	/**
	 * 根据jobId查询日志列表
	 * 
	 * @param jobId
	 * @return
	 */
	public List<JobStepLogView> listJobStepLogs(Long jobId) {
		
		// 查询step日志
		YYJobStepLogDOExample example = new YYJobStepLogDOExample();
		example.createCriteria().andJobIdEqualTo(jobId).andIsDeletedEqualTo("N");
		example.setOrderByClause("exec_start_time asc, gmt_created asc");
		List<YYJobStepLogDO> jobStepLogList = stepLogDOMapper.selectByExample(example);
		
		// 查询步骤列表
		YYJobStepDOExample stepDOExample = new YYJobStepDOExample();
		stepDOExample.createCriteria().andJobIdEqualTo(jobId);
		List<YYJobStepDO> jobStepList = jobStepDOMapper.selectByExample(stepDOExample);
		Map<Long, YYJobStepDO> jobStepMap = jobStepList.stream().collect(Collectors.toMap(k->k.getId(), v->v));
		
		return JobStepLogViewConverter.listConvert(jobStepLogList, jobStepMap);
	}

}
